package com.easy.facilitaterecycleadapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.easy.facilitaterecycleadapter.adapter.DefaultLoaderCreator;
import com.easy.facilitaterecycleadapter.adapter.DefaultRefershCreator;
import com.easy.facilitaterecycleadapter.adapter.OnLoadMoreListener;
import com.easy.facilitaterecycleadapter.adapter.OnRefreshListener;
import com.easy.facilitaterecycleadapter.adapter.RefreshAndLoaderRecyclerView;
import com.easy.facilitaterecycleadapter.adapter.TestAdapter;
import com.easy.facilitaterecycleadapter.decoration.CustomGrideLayoutDecoration;
import com.easy.facilitaterecycleadapter.decoration.CustomLineLayoutDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private RefreshAndLoaderRecyclerView mRecycView;
    private List<Integer> list =new ArrayList<>();
    private CustomLineLayoutDecoration lineLayoutDecoration;
    private CustomGrideLayoutDecoration grideLayoutDecoration;
    private TestAdapter testAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initRecycView();

    }

    private void initRecycView() {
        mRecycView = (RefreshAndLoaderRecyclerView) findViewById(R.id.recyc_view);
        View emptyView = findViewById(R.id.empty_layout);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this,3);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return setSpanSize(position);
            }
        });


        mRecycView.setLayoutManager(gridLayoutManager);
        testAdapter = new TestAdapter(list, MainActivity.this, R.layout.base_item_layout);
        mRecycView.setAdapter(testAdapter);
        mRecycView.addEmptyView(emptyView);
        addReLo();
    }

    private int setSpanSize(int position) {
        int count;
        if (position ==0 || position==list.size()+1) {
            count = 3;
        } else {
            count = 1;
        }
        return count;
    }

    private void initData() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for(int x=0;x<20;x++){
                            list.add(x);
                        }
                        testAdapter.notifyDataSetChanged();
                    }
                });

            }
        },2000);

    }

    public void addReLo(){
//        mRecycView.addHanderView(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_hand,mRecycView,false));
        mRecycView.addViewCreator(new DefaultRefershCreator());
        mRecycView.addViewCreator(new DefaultLoaderCreator());
        mRecycView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mRecycView.onStopRefresh();
                            }
                        });

                    }
                },2000);
            }
        });
        mRecycView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoad() {

                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                for(int x=0;x<20;x++){
                                    list.add(x);
                                }
                                testAdapter.notifyDataSetChanged();
                                mRecycView.onStopLoad();
                            }
                        });
                    }
                },500);
            }
        });
    }

}
